import {getCatesByPid, getGoodsCatePage} from "../../../service/goods";
import list_behavior from "../../../behavior/list_behavior";

Component({
    behaviors:[list_behavior],
    properties: {
        cates: {type:Array, value:[]},
        height:{type:String,value:'100%'},
        type:{type:String, value:'goods'},  // 两种展示样式： goods:展示商品，cate：只展示分类,
        selectOneId:{
            type:Number,value:0,
        },
    },
    data: {
        diyPage:{},
        selectTwoList:[],
        selectOneIndex:0,
        // selectCate:null,
        // selectTwoIndex:0,
        // contentHeight:'',
    },
    lifetimes:{
        attached() {
            const {cates, selectOneIndex} = this.data
            const oneCateId = cates[selectOneIndex].id
            // 设置选中的二级分类
            this.setTwoList(oneCateId)
            // 分类页模板配置
            getGoodsCatePage().then(r => {

                this.setData({
                    diyPage:r
                })
                // 根据分类页模板判断是否展示商品列表
                if (r.tpl == 'tpl_1') {
                    this.setData({
                        [`list.url`]:'/goods/paginate',
                        [`list.where`]:{
                            cate_id: oneCateId
                        }
                    })
                    this.setList(1)
                }
            })
        }
    },
    observers:{
        'height, type':function (height, type) {
            let ch = ''
            if (type == 'goods'){
                ch = `calc(${height} - 128rpx - 100rpx)`
            }else if(type == 'cate'){
                ch = `100%`
            }

            this.setData({
                contentHeight:ch
            })
        },
        'selectOneId,cates': function (selectOneId,cates){
            if (selectOneId && cates.length>0){
                const index = cates.findIndex(item => item.id==selectOneId)
                this.setData({
                    selectOneIndex:index,
                })
            }
        },

    },
    methods: {

        searchClick(e){
            this.triggerEvent('change',{
                type:'search',
                key: e.detail.value,
            })
        },

        sideSelect(e){
            const {index} = e.detail
            const {cates} = this.data
            const oneCate = cates[index]

            this.setData({
                selectOneIndex:index,
            })
            this.setTwoList(oneCate.id)
        },

        tabTwoChange(e){
            const {index, twoCate} = e.detail
            const {selectOneIndex, diyPage, cates} = this.data
            const oneCate = cates[selectOneIndex]

            if (diyPage.tpl == 'tpl_1') {   // 展示商品
                const where = this.data.list.where
                const twoCateId = twoCate.id
                if (twoCateId == -1){   // -1代表全部，直接按上级id查询
                    where.cate_id = oneCate.id
                }else{
                    where.cate_id = twoCateId
                }
                this.setData({
                    [`list.where`]:where
                })
                this.setList(1)
            }

            // this.triggerEvent("change", {
            //     oneIndex:selectOneIndex,
            //     towIndex:index,
            //     cate:twoCate,
            // })
        },

        tagTwoChange(e){
            const {index, twoCate} = e.detail
            const {selectOneIndex} = this.data

            this.triggerEvent("change", {
                oneIndex:selectOneIndex,
                towIndex:index,
                cate:twoCate,
            })
        },

        tagThreeChange(e){
            const {index, twoCate, threeCate} = e.detail
            const {selectOneIndex} = this.data

            this.triggerEvent("change", {
                oneIndex:selectOneIndex,
                towIndex:index,
                cate:threeCate,
            })
        },

        setTwoList(pid){
            const {list, diyPage} = this.data
            const where = list.where
            getCatesByPid(pid).then(r => {
                this.setData({
                    selectTwoList:r
                })
                // 如果页面模板是tpl_1 需要展示对应的商品
                if (diyPage.tpl == 'tpl_1'){
                    where.cate_id = pid
                    this.setData({
                        [`url.where`]:where
                    })
                    this.setList(1)
                }
            })
        },

    }
});
